package com.jvyou.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

@Service
public class MyTaskService {

    @Autowired
    private TaskExecutor taskExecutor;

    private record MessagePrinterTask(String message) implements Runnable {

        public void run() {
            System.out.println(message);
        }
    }

    public void executeTasks() {
        for (int i = 0; i < 25; i++) {
            taskExecutor.execute(new MessagePrinterTask("Message" + (i + 1)));
        }
    }


    @Autowired
    private TaskScheduler taskScheduler;

    public void scheduleTask() {
        LocalDateTime futureDateTime = LocalDateTime.now().plusSeconds(10);
        Instant instant = futureDateTime.atZone(ZoneId.systemDefault()).toInstant();

        taskScheduler.schedule(() -> {
            System.out.println("Scheduled task executed at: " + LocalDateTime.now());
        }, instant);
    }
}
